package com.cyj.controller;

import com.cyj.entity.AnimeEnshrine;
import com.cyj.entity.FileEntity;
import com.cyj.service.AnimeEnshrineService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/enshrine")
public class AnimeEnshrineController {

    @Autowired
    private AnimeEnshrineService animeEnshrineService;

    @RequestMapping("/enshrines")
    public String regions(Locale locale){

        return "animeEnshrineList";
    }

    @ResponseBody
    @RequestMapping(value = "/animeEnshrineList",produces = "application/json;charset=utf-8")
    public Map animeEnshrine(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model) throws IOException {
        Map map = new HashMap<>();

        PageInfo page  = animeEnshrineService.selectAnimeEnshrineList(map,pn,5);

        map.put("pageInfo",page);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/enshrineBy",produces = "application/json;charset=utf-8")
    public Map enshrineBy(HttpServletRequest request){
        Map<String,Object> map = new HashMap<String,Object>();
        Integer enshrineId = Integer.parseInt(request.getParameter("enshrineId"));

        map.put("enshrineId",enshrineId);

        AnimeEnshrine animeEnshrine = animeEnshrineService.selectAnimeEnshrine(map);

        System.out.println(animeEnshrine);

        if(animeEnshrine!=null){
            map.put("code", 200);
            map.put("msg", "追番查找成功");
            map.put("data",animeEnshrine);
        }else {
            map.put("code", 100);
            map.put("msg", "追番查找失败");
        }

        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/updateAnimeEnshrine",produces = "application/json;charset=utf-8")
    public Map updateAnimeEnshrine(HttpServletRequest request, FileEntity entity){

        Map<String, String[]> map = request.getParameterMap();

        AnimeEnshrine animeEnshrine = new AnimeEnshrine();

        try {
            BeanUtils.populate(animeEnshrine,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        System.out.println(animeEnshrine);

        int num = animeEnshrineService.updateAnimeEnshrine(animeEnshrine);

        Map<String,Object> map2 = new HashMap<String,Object>();

        if (num>0){
            map2.put("code", 200);
            map2.put("msg", "追番修改成功");
        }else {
            map2.put("code", 100);
            map2.put("msg", "追番修改失败");
        }

        return map2;
    }

    @ResponseBody
    @RequestMapping(value = "/addAnimeEnshrine",produces = "application/json;charset=utf-8")
    public Map addAnimeEnshrine(@RequestBody AnimeEnshrine animeEnshrine){

        Map<String,Object> map = new HashMap<String,Object>();

        animeEnshrine.setEnshrineTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
        System.out.println(animeEnshrine);

        int num = animeEnshrineService.addAnimeEnshrine(animeEnshrine);


        if(num>0){
            map.put("code",200);
            map.put("msg","添加追番成功！");
        }else{
            map.put("code",100);
            map.put("msg","添加追番失败！");
        }

        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/delAnimeEnshrine",produces = "application/json;charset=utf-8")
    public Map delAnimeEnshrine(@RequestBody AnimeEnshrine animeEnshrine){

        Map<String,Object> map = new HashMap<String,Object>();

        System.out.println(animeEnshrine);

        int num = animeEnshrineService.deleteAnimeEnshrine(animeEnshrine.getEnshrineId());

        if(num>0){
            map.put("code",200);
            map.put("msg","追番删除成功！");
        }else{
            map.put("code",100);
            map.put("msg","追番删除失败！");
        }


        return map;
    }

}
